package com.game.worm.mobile.view.home;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.dx.as.http.IHttpListener;
import com.game.worm.mobile.tools.CustomDialog;
import com.dx.as.fragment.TFragment;
import com.dx.as.ui.ad.CustomBanner;
import com.game.worm.mobile.R;
import com.game.worm.mobile.bean.GameBean;
import com.game.worm.mobile.view.GameExtras;
import com.game.worm.mobile.view.GameHelper;
import com.game.worm.mobile.view.home.adpater.HotAdpater;
import com.game.worm.mobile.view.home.adpater.GamedAdpater;
import com.game.worm.mobile.view.http.HttpHelper;
import com.game.worm.mobile.view.tags.TagsActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by d_xxiang on 2018/3/27.
 */

public class HomeFragment extends TFragment{
    private CustomBanner<GameBean> customBanner;
    private HotAdpater hotAdpater;
    private GamedAdpater gamedAdpater;
    @Override
    public void initView() {
        customBanner = findView(R.id.banner);

        findView(R.id.rb_all).setOnClickListener(onClickListener);
        findView(R.id.rb_crack).setOnClickListener(onClickListener);
        findView(R.id.rb_discount).setOnClickListener(onClickListener);
        findView(R.id.rb_demo).setOnClickListener(onClickListener);
        findView(R.id.rb_bt).setOnClickListener(onClickListener);
        findView(R.id.hot_all).setOnClickListener(onClickListener);

        RecyclerView recyclerViewHot = findView(R.id.recyclerview_hot);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewHot.setLayoutManager(linearLayoutManager);

        hotAdpater = new HotAdpater(getActivity());
        recyclerViewHot.setAdapter(hotAdpater);

        RecyclerView recyclerViewRecommend = findView(R.id.recyclerview_recommend);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getActivity());
        linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerViewRecommend.setLayoutManager(linearLayoutManager2);
        gamedAdpater = new GamedAdpater(getActivity());
        recyclerViewRecommend.setAdapter(gamedAdpater);
        getHomeData();
//        View view = View.inflate(getActivity(),R.layout.dialog_login,null);
//        CustomDialog dialog = new CustomDialog.Builder(getActivity()).builder(view);
//        dialog.show();
    }

    private void getHomeData(){
        HttpHelper.getHomeInfo(iHttpListener);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden){
            hideToolbar(false);
        }
    }



    @Override
    public int setLayoutI() {
        return R.layout.fragment_home;
    }

    @Override
    public void dataOpion() {
    }

    @Override
    public void initRpo() {

    }


        //设置没有指示器
    private void setApdater(final List<GameBean> beans) {
        customBanner.setPages(new CustomBanner.ViewCreator<GameBean>() {
            @Override
            public View createView(Context context, int position) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_banner,null,false);
                return view;
            }

            @Override
            public void updateUI(Context context, View view, int position, GameBean entity) {
                ImageView imge = view.findViewById(R.id.img_ad);
                ImageLoader.getInstance().displayImage(entity.getBannerImgUrl(),imge);
                TextView name = view.findViewById(R.id.name);
                name.setText(entity.getName());
                TextView intro = view.findViewById(R.id.intro);
                intro.setText(entity.getIntro());
            }
        }, beans)
                //设置没有指示器
                .setIndicatorStyle(CustomBanner.IndicatorStyle.NONE)
                //设置自动翻页
                .startTurning(5000);
        customBanner.setOnPageClickListener(new CustomBanner.OnPageClickListener() {
            @Override
            public void onPageClick(int position, Object o) {
                GameBean bean = (GameBean)o;
                GameHelper.goToGameDetailsView(bean.getId());
            }
        });
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String tag ="";
            String tagName = v instanceof RadioButton ? ((RadioButton)v).getText().toString() : "";
            int type = GameExtras.EXTRAS_GAME_BY_TYPE;
            switch (v.getId()) {
                case R.id.rb_bt: //BT游戏
                    tag = "19";
                    break;
                case R.id.rb_discount://折扣游戏
                    tag = "20";
                    break;
                case R.id.rb_demo://有奖试玩
                    tag = "22";
                    break;
                case R.id.rb_crack://汉化破解
                    tag = "21";
                    break;
                case R.id.rb_all://全部分类
                    GameHelper.startNewIntent(TagsActivity.class);
                    break;
                case R.id.hot_all://全部（本周）热门
                    type = GameExtras.EXTRAS_GAME_BY_HOT;
                    tagName = getString(R.string.week_hot);
                    break;
            }

            if (v.getId() != R.id.rb_all)
                GameHelper.goToTagGameListView(tag,tagName, type);
        }
    };


    private IHttpListener iHttpListener = new IHttpListener() {
        @Override
        public void onFilure(int code, String errorMsg) {
            super.onFilure(code, errorMsg);
        }

        @Override
        public void onResponse(Object response, int flg) {
            if (response != null){
                HomeInfo homeInfo = (HomeInfo)response;

                gamedAdpater.addData(homeInfo.getRecommend());
                gamedAdpater.notifyDataSetChanged();

                setApdater(homeInfo.getBanner());


                hotAdpater.addData(homeInfo.getHot());
                hotAdpater.notifyDataSetChanged();

            }
        }

        @Override
        public void onFailure(Throwable t) {
            super.onFailure(t);
        }
    };
}
